﻿using CommunicationProtocol.dto;
using CommunicationProtocol.dto.command.server;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using monoClient.utils;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework.Input;
using static CommunicationProtocol.utils.PixelsLocationToColumnRowTransformer;

namespace monoClient.renderer
{
    internal class AnimatePlantRenderer : CommandExecutor
    {
        List<Texture2D> texture2Ds;
        int milliSecondPerImg;
        double milliSecSum;
        int frameIndex = 0;

        Rectangle plantLocation;

      

        public AnimatePlantRenderer(BaseObject baseObject)
        {

        
            texture2Ds = ImageUtils.loadImagesByObjectType(baseObject.ObjectType);
            milliSecondPerImg = 1000 / texture2Ds.Count;
            toPixels(baseObject.LocationX, baseObject.LocationY, out int xPixel, out int yPixel);

            xPixel += aLandWidth / 2+aLandWidth;
            yPixel += (int)(aLandHeight * 0.7);
            
            Debug.WriteLine($"要种植植物在{baseObject.LocationX}列{baseObject.LocationY}行,像素是{xPixel},{yPixel}");
            
            plantLocation = new Rectangle(xPixel, yPixel, aLandWidth, aLandHeight);
        }

        public void Update(GameTime gameTime, MouseState mouseState, MouseState oldMouseState)
        {
            milliSecSum += gameTime.ElapsedGameTime.TotalMilliseconds;

            if (milliSecSum > milliSecondPerImg) { 
            
                milliSecSum = 0;
                frameIndex= (frameIndex+1)%texture2Ds.Count;
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch _spriteBatch)
        {
            _spriteBatch.Draw(texture2Ds[frameIndex], plantLocation,Color.White);
        }

     

    }
}
